<template>
    <div>
        <!-- 有子产品，统一发版的-->
        <HCollapse collapse @change="handleLoadData">
            <template #title>
                <flexbox style="color: #70798b">
                    <span class="font-w-b">{{ detail.versionNumber }} </span>
                    <flexbox style="flex: 1; white-space: nowrap; overflow: hidden" justify="center">
                        <span class="text-bre1"> {{ detail.startTime }}~{{ detail.onlineTime }} </span>
                    </flexbox>
                </flexbox>
            </template>
            <div v-for="item of subVersionSystemList" :key="item.id">
                <!-- 每一个具体产品或者子产品 -->
                <SubVersionSystemItem :systemDetail="item" :disabled="disabled" :signPostShowType="signPostShowType" @success="handleLoadData" />
            </div>
            <template #titleExtra>
                <flexbox justify="flex-end" align="center" style="width: 100%; color: #333b4b" v-if="!disabled">
                    <flexbox>
                        <my-icon type="iconbianji" />
                        <span style="margin-left: 4px" @click.stop="handleEdit">编辑</span>
                    </flexbox>
                    <flexbox class="margin-l-16">
                        <my-icon type="icondelete" />
                        <span style="margin-left: 4px" @click.stop="handleDelete">删除</span>
                    </flexbox>
                </flexbox>
            </template>
        </HCollapse>
        <CreateVersionModal ref="CreateVersionModalRef" :lubiaoDetail="lubiaoDetail" :subVersionInfo="subVersionInfo" @success="handleSuccess" />
    </div>
</template>

<script lang="ts">
import SubVersionSystemItem from './SubVersionSystemItem.vue';
import CreateVersionModal from '../../VersionModal/CreateVersionModal.vue';
import api from '@/api';
import { message } from 'ant-design-vue';
import { useMessage } from '@/hooks/common/useMessage';
import HCollapse from '../components/HCollapse.vue';
import { defineComponent, reactive, toRefs, ref, unref, getCurrentInstance } from 'vue';
export default defineComponent({
    components: {
        HCollapse,
        SubVersionSystemItem,
        CreateVersionModal
    },
    props: {
        detail: {
            type: Object,
            default: () => ({})
        },
        lubiaoDetail: {
            type: Object,
            default: () => ({})
        },
        signPostShowType: {
            type: String,
            default: ''
        },
        subVersionInfo: {
            type: Object,
            default: () => ({})
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    emits: ['success'],
    setup(props, { emit }) {
        const instance: any = getCurrentInstance();
        const CreateVersionModalRef = ref<any>();
        const state = reactive<any>({
            subVersionSystemList: []
        });
        const handleLoadData = async (expanded) => {
            if (expanded) {
                const query = {
                    versionNumber: props.detail.versionNumber,
                    catalogCode: props.detail.catalogCode
                };
                const { success, moule } = await api.versionModel.findByVerNoAndCatalogCode(query);
                if (success) {
                    state.subVersionSystemList = [...moule];
                }
            }
        };
        const handleEdit = () => {
            unref(CreateVersionModalRef).open(props.detail);
        };
        const handleDelete = () => {
            const { createConfirm } = useMessage();
            createConfirm({
                title: `确认删除【${props.detail.versionNumber}】？`,
                iconType: 'danger',
                onOk: async () => {
                    const query = {
                        id: props.detail.id
                    };
                    const { success, errorMsg } = await api.versionControlModel.delete(query);
                    if (success) {
                        if (props.detail.versionIds) {
                            const { success, errorMsg } = await api.versionModel.deleteById({
                                id: props.detail.versionIds
                            });
                            if (success) {
                                handleSuccess();
                                message.success('删除成功');
                            } else {
                                message.error(errorMsg);
                            }
                        } else {
                            handleSuccess();
                            message.success('删除成功');
                        }
                    } else {
                        message.error(errorMsg);
                    }
                }
            });
        };
        const handleSuccess = () => {
            emit('success');
        };
        // 更新展开接口
        instance.proxy.$mitt.on('taskRequireList-subVersionSystemList-loadData', () => {
            handleLoadData(true);
        });
        return {
            CreateVersionModalRef,
            ...toRefs(state),
            handleLoadData,
            handleEdit,
            handleDelete,
            handleSuccess
        };
    }
});
</script>
<style lang="less" scoped>
::v-deep(.ant-collapse-content > .ant-collapse-content-box) {
    padding-bottom: 2px !important;
}
</style>